local CommonOHOH = _G.CommonOHOH;
local M = CommonOHOH.class.new(script, CommonOHOH.files.BaseObject());
local ConnectClass=_G.CommonOHOH.load.Connect();

local BodyClass=require(script.Part.Body)
local FlagClass=require(script.Part.Flag)
local TailClass=require(script.Part.Tail)
local RingClass=require(script.Part.Ring)
local WeaponClass=require(script.Part.Weapon)




-- 初始化一辆车
function M.new(...)
    print("#### NEW")
	return M(...);
    
end;

function M.ctor(self,bodyId,flagId,ringId,tailId,weaponId)--构造函数
	M.super.ctor(self);
    print("#### ctor")
    self.Model = Instance.new("Model")
    self.Model.Name = "testCar" 
    self.Model.Parent = workspace

    self.Body=BodyClass.new(self)--车身
    self.Flag=FlagClass.new(self)--装饰
    self.Tail=TailClass.new(self)--轮胎
    self.Ring=RingClass.new(self)--推进器
    self.Weapon=WeaponClass.new(self)--钻头

    self.BodyUpdateEvent=ConnectClass.new() --车身发生更新事件
    self.FlagUpdateEvent=ConnectClass.new() --装饰发生更新事件
    self.TailUpdateEvent=ConnectClass.new() --推进器发生更新事件
    self.RingUpdateEvent=ConnectClass.new() --轮胎发生更新事件
    self.WeaponUpdateEvent=ConnectClass.new() --钻头发生更新事件

    --测试
    --测试
    if bodyId then
        
        self:UpdateBody(bodyId)
    end

    if flagId then
        self:UpdateFlag(flagId)
    end

    if ringId then
        self:UpdateRing(ringId)
    end

    if tailId then
        self:UpdateTail(tailId)
    end

    if weaponId then
        self:UpdateWeapon(weaponId)
    end

    self:UpdateAll()
end;


function M.dtor(self)
    self.Body:dtor()
    self.Flag:dtor()
    self.Ring:dtor()
    self.Weapon:dtor()
    self.Tail:dtor()
	M.super.dtor(self);
end;

--设置车的位置（需实现）
function M.SetCFrame(self,cf)
     self.Model:PivotTo(cf)
end

--设置车的父节点（需实现）
function M.SetParent(self,parent)
     self.Model.Parent = parent
end

--获取车的根节点，如果是模型则是主要节点（需实现）
function M.GetRoot(self)
    --设置为Body
    return self.Model.Body
end

--设置车的前进速度（需实现）
function M.SetForwardSpeed(self,speed)
    --速度修改测试
    -- local wtime =30
 
    if not self.Model.Base.Car:FindFirstChild("BodyVelocity") then
      local  bodyVelocity = Instance.new("BodyVelocity",self.Model.Base.Car)
    end
	local  carDirect = - self.Model:GetPivot().RightVector.Unit

	self.Model.Base.Car.BodyVelocity.velocity =  carDirect* speed
	self.Model.Base.Car.BodyVelocity.maxForce =carDirect*500*35

	-- wait(wtime)
	-- bodyVelocity:Destroy()
end

-- 设置车的转速（需实现）
function M.SetDrillingSpeed(self,speed)
    self.Model.Body.hinge.AngularVelocity = speed          -- 设置马达速度
	self.Model.Body.hinge.MotorMaxTorque = 3000    -- 设置最大扭矩
end


-----------更新部件---------------
-- 更新车身
function M.UpdateBody(self,id)
    if self.bodyId and  self.bodyId==id then
        return 
    end
    self.bodyId=id
    self.Body:Update(self.bodyId)
    self.BodyUpdateEvent:Action(self.bodyId)

end

-- 更新推进器
function M.UpdateTail(self,id)
    if self.tailId and  self.tailId==id then
        return 
    end
    self.tailId=id
    self.Tail:Update(self.tailId)
    self.TailUpdateEvent:Action(self.tailId)
end


-- 更新钻头
function M.UpdateWeapon(self,id)
    if self.weaponId and  self.weaponId==id then
        return 
    end
    self.weaponId=id
    self.Weapon:Update(self.weaponId)
    self.WeaponUpdateEvent:Action(self.weaponId)
end

-- 更新轮胎
function M.UpdateRing(self,id)
    if self.ringId and  self.ringId==id then
        return 
    end
    self.ringId=id
    self.Ring:Update(self.ringId)
    self.RingUpdateEvent:Action(self.ringId)
end


-- 更新装饰
function M.UpdateFlag(self,id)
    if self.flagId and  self.flagId==id then
        return 
    end
    self.flagId=id
    self.Flag:Update(self.flagId)
    self.FlagUpdateEvent:Action(self.flagId)

end

function M.UpdateAll(self)
--美术车辆组装后属性设置
    local carParts =self.Model:GetDescendants()
    for _, child in pairs( carParts) do	
        if child.ClassName == "MeshPart"  then
            child.Massless = false
            child.Anchored = false
            --child.Anchored = true
            child.CanQuery = false
            child.CanCollide = false
            child.CanTouch = false
            
        end
    end
    print("#### carParts =self.Model:GetDescendants()",carParts)
---创建车子的真实驱动部分
    --base
    local  base= CommonOHOH:GetPath_Loop("ReplicatedStorage.CarBase.Base"):Clone()
    base.Name = "Base"
    
    local ringLocate =self.Body.Model.Ring:GetPivot()
    local ringRY = self.Ring.Model.Instance.Size.Y/2
    local baseRY = base.Car.BL.Size.Y/2

    local ringPosition = ringLocate.Position
    local basePosition = ringPosition - Vector3.new(0, ringRY, 0) + Vector3.new(0, baseRY, 0)
    local newBaseCFrame = CFrame.new(basePosition) * CFrame.Angles(base:GetPivot():ToEulerAnglesXYZ())
    base:PivotTo(newBaseCFrame)
	base.Parent= self.Model

    local weld = Instance.new("WeldConstraint")
	weld.Part0 = self.Model.Body
	weld.Part1 =  base.Car
	weld.Parent = self.Model.Body
    self.Model.Body.Anchored = false
    base.Car.Anchored =false

    --ring
    local currings = self.Model.Body.Ring:GetChildren()
    for _, child in pairs( currings) do
    	if child.ClassName == "MeshPart"  then
    		local attAO = Instance.new("Attachment")
    		attAO.Name = "attachAlign"
    		attAO.Parent = child
    		local alignO  = Instance.new("AlignOrientation")
    		alignO.Attachment0 = attAO
    		alignO.Attachment1 = base.Car.BL.Attachment1
    		alignO.Parent = child
    	end
    end
    
    local seat = Instance.new("Seat")
    seat.CFrame  = self.Body.Model.Player:GetPivot()
    seat.Size = Vector3.new(0.1,0.1,0.1)
    -- seat.Transparency = 1
    local weld = Instance.new("WeldConstraint")
	weld.Part0 = seat
	weld.Part1 =  base.Car
	weld.Parent = seat
    seat.Parent = base
    local  bodyVelocity = Instance.new("BodyVelocity",self.Model.Base.Car)
    
    --self.Model.PrimaryPart = self.Model.Body
    -----------------------------------------------
 
     local currentCFrame = CFrame.new(Vector3.new(9.284, 5.264, -15.52)) 
    local newXRotation = math.rad(0) -- 新的 X 轴旋转（弧度）
    local newYRotation = math.rad(45) -- 新的 Y 轴旋转（弧度）
    local newZRotation = math.rad(0) -- 新的 Z 轴旋转（弧度）

    local newCFrame = CFrame.new(currentCFrame.Position) * CFrame.Angles(newXRotation, newYRotation, newZRotation)
    self.Model:PivotTo(newCFrame)
  
    self:SetForwardSpeed(200)
    ------------------------------------------------------
		
	
end

--------------获取部件----------------------------
-- 获取车身对象
function M.GetBody(self)
    return  self.Body
end
-- 获取推进器对象
function M.GetTail(self)
    return  self.Tail
end
-- 获取钻头对象
function M.GetWeapon(self)
    return  self.Weapon
end
-- 获取轮胎对象
function M.GetRing(self)
    return  self.Ring
end
-- 获取装饰对象
function M.GetFlag(self)
    return  self.Tail
end

return M